package com.ysstech.activiti.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.ysstech.activiti.service.ActivitiRuntimeService;
import com.ysstech.activiti.service.ActivitiTaskService;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@Controller
@Api(tags = "流程运行时api")
@RequestMapping(value = "/activitiRuntime")
public class ActivitiRuntimeController {
    @Autowired
    private ActivitiRuntimeService activitiRuntimeService;


    /**
     * 启动流程
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/startProcessFlow")
    @ResponseBody
    public RestResult startProcessFlow(String userId, String key, String id) throws Exception {
        activitiRuntimeService.startProcessFlow(userId, key, id);
        return ResultGenerator.getSuccessResult("启动流程定义成功");
    }

    /**
     * 查看当前流程图（查看当前活动节点，并使用红色的框标注）
     */
    @RequestMapping(value = "/viewCurrentImage")
    public String viewCurrentImage(String taskId, HttpServletRequest request) throws Exception {
        /**查看当前活动，获取当期活动对应的坐标x,y,width,height，将4个值存放到Map<String,Object>中*/
        Map acs = activitiRuntimeService.queryProcessFlowByTask(taskId);
        request.setAttribute("acs", acs);
        request.setAttribute("deploymentId", acs.get("deploymentId"));
        request.setAttribute("imageName", acs.get("imageName"));
        return "activiti/image";
    }
}
